﻿Imports VN.INS.BL.BC
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO

Partial Class Devida_ListaPOAs
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then

            Sesion(Session("Persona"))
            Sesion(Session("perfil"))
            ViewState("Persona") = Session("Persona")
            ViewState("Perfil") = Session("perfil")
            ViewState("NUM") = 0
            ListaTipo()
            CargarGrilla()

            'By Tigrito (26/07/2013): Desabilitando botones
            ibAFinanciero.Visible = False
            ibAFisico.Visible = False
            exportar.Visible = False

        End If
    End Sub

    'Carga de grilla
    Public Sub CargarGrilla()
        Dim objPOABC As POABC
        Dim objData As DataTable
        objPOABC = New POABC
        Dim strPersona As String = ""

        If Session("perfil") = "AD" Or Session("perfil") = "RE" Then

        Else
            strPersona = ViewState("Persona")
        End If

        objData = objPOABC.ListaPOA(strPersona, Session("Año"))
        gv_impresion.DataSource = objData
        gv_impresion.DataBind()
        gv_doc.DataSource = objData
        gv_doc.DataBind()
    End Sub

    'Carga de grilla con filtro
    Public Sub CargarGrilla(ByVal strPoa As String, ByVal g_strPersona As String, ByVal strNombre As String, ByVal strTipo As String)
        Dim objPOABC As POABC
        Dim objData As DataTable
        objPOABC = New POABC
        Dim strPersona As String = ""

        'If Session("Perfil") = "AD" Or Session("Perfil") = "RE" Then
        If New String() {"AD", "RE"}.Contains(Session("Perfil")) Then
            objData = objPOABC.ListaPOA(strPoa, "", strNombre, strTipo, Session("Año"))
        Else
            strPersona = g_strPersona
            objData = objPOABC.ListaPOA(strPoa, strPersona, strNombre, strTipo, Session("Año"))
        End If

        gv_impresion.DataSource = objData
        gv_impresion.DataBind()
        gv_doc.DataSource = objData
        gv_doc.DataBind()
    End Sub

    Public Sub ListaTipo()
        Dim objMonitorBC As MONITOREOBC = New MONITOREOBC
        Dim dttip As DataTable
        dttip = objMonitorBC.tipoProyectoActividad()
        Dim drttiprow As DataRow = dttip.NewRow()
        drttiprow("DESCRIPCION") = "--Seleccione--"
        drttiprow("TIPO_PROYECTO_ACTIVIDAD") = "all"
        dttip.Rows.Add(drttiprow)
        ddlTipo.DataSource = dttip
        ddlTipo.DataTextField = "DESCRIPCION"
        ddlTipo.DataValueField = "TIPO_PROYECTO_ACTIVIDAD"
        ddlTipo.DataBind()
        ddlTipo.SelectedValue = "all"
    End Sub

    Protected Sub ibAFinanciero_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibAFinanciero.Click
        'redirecciona avance financiero del poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("POAFinanciero.aspx?Param=" + ViewState("POA").ToString + "&NomEjecutor=" + ViewState("NomEjecutor") + "&NomProyAct=" + ViewState("NomProyAct"))

        End If
    End Sub

    Protected Sub ibAFisico_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibAFisico.Click
        'redirecciona avance fisico del poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("POAFisico.aspx?Param=" + ViewState("POA").ToString + "&NomEjecutor=" + ViewState("NomEjecutor") + "&NomProyAct=" + ViewState("NomProyAct"))

        End If
    End Sub

    'salir
    Protected Sub ibn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibn_salir.Click
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub buscar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles buscar.Click
        CargarGrilla(txtPOA.Text.ToString, ViewState("Persona"), txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString)
    End Sub

    Protected Sub gv_impresion_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_impresion.RowDataBound
        ' FORMATEA ROWS
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ASIGNA EVENTOS
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gv_impresion, "Select$" + e.Row.RowIndex.ToString)

            'Esto es para el campo Año Firma 
            Select Case e.Row.Cells(2).Text
                Case "2012"
                    e.Row.Cells(2).BackColor = Drawing.Color.LightGray
                Case "2013"
                    e.Row.Cells(2).BackColor = Drawing.Color.DarkTurquoise
                    e.Row.Cells(2).ForeColor = Drawing.Color.Black
                Case "2014"
                    e.Row.Cells(2).BackColor = Drawing.Color.MediumPurple
                    e.Row.Cells(2).ForeColor = Drawing.Color.White
                Case Else
                    e.Row.Cells(2).BackColor = Drawing.Color.LightGray
            End Select

            'Esto es para el estado de reprogramación/ reformulación del POA
            If e.Row.Cells(7).Text = "PROCESO REPROG." Then
                e.Row.Cells(7).Text = "PROCESO INGRESO POA"
                e.Row.Cells(7).BackColor = Drawing.Color.Black
                e.Row.Cells(7).ForeColor = Drawing.Color.Orange
            End If

        End If
    End Sub

    Protected Sub gv_impresion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_impresion.SelectedIndexChanged
        'Captura variables de fila seleccionada
        Dim htxtPOA As HiddenField = CType(gv_impresion.SelectedRow.FindControl("POA"), HiddenField)
        Dim htxtNomEjecutor As HiddenField = CType(gv_impresion.SelectedRow.FindControl("NomEjecutor"), HiddenField)
        Dim htxtNomProyAct As HiddenField = CType(gv_impresion.SelectedRow.FindControl("NomProyAct"), HiddenField)

        Dim strNombre As String = gv_impresion.SelectedRow.Cells(4).Text.ToString

        Try
            ViewState("Nombre") = strNombre
            If Not htxtPOA Is Nothing Then
                ViewState("POA") = htxtPOA.Value.ToString
            End If

            If Not htxtPOA Is Nothing Then
                ViewState("NomEjecutor") = htxtNomEjecutor.Value.ToString
            End If

            If Not htxtPOA Is Nothing Then
                ViewState("NomProyAct") = htxtNomProyAct.Value.ToString
            End If


        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub ExportarGrilla(ByVal grilla As GridView)
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        ViewState("NUM") = ViewState("NUM") + 1
        'CREAR TITULO
        Dim gvtitle As New Table
        Dim titulo1 As New TableCell()
        titulo1.RowSpan = 1
        titulo1.ColumnSpan = 10
        titulo1.Text = "LISTA DE PLANES OPERATIVOS ANUALES"
        titulo1.Font.Name = "Calibri"
        titulo1.Font.Size = 12
        titulo1.Font.Bold = True
        titulo1.BackColor = Drawing.Color.White
        titulo1.HorizontalAlign = HorizontalAlign.Center
        titulo1.VerticalAlign = VerticalAlign.Middle
        Dim headertitulo1 As New TableRow()
        headertitulo1.Cells.Add(titulo1)
        gvtitle.Rows.AddAt(0, headertitulo1)
        '------------
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(gvtitle)
        form.Controls.Add(grilla)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=Lista_POA_" + ViewState("NUM").ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()
    End Sub

    Protected Sub exportar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles exportar.Click
        ExportarGrilla(gv_doc)
    End Sub

    Protected Sub txt_numero_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_numero.TextChanged
        CargarGrilla(txtPOA.Text.ToString, ViewState("Persona"), txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString)
    End Sub

    Protected Sub ibProgFin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibProgFin.Click
        'redirecciona metas financieras del poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("ProgFinanciero.aspx?Param=" + ViewState("POA").ToString + "&NomEjecutor=" + ViewState("NomEjecutor") + "&NomProyAct=" + ViewState("NomProyAct"))

        End If
    End Sub

    Protected Sub ibProgFis_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibProgFis.Click
        'redirecciona metas fisicas del poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("ProgFisico.aspx?Param=" + ViewState("POA").ToString + "&NomEjecutor=" + ViewState("NomEjecutor") + "&NomProyAct=" + ViewState("NomProyAct"))

        End If
    End Sub

    Public Sub Sesion(ByVal session As Object)
        'perdida de sesion
        Dim lost As String
        lost = ""
        If (session Is Nothing) Then lost = "1" Else lost = "0"
        If lost = "1" Then
            Response.Redirect("s_login.aspx?Error=1")
        End If
    End Sub

End Class



